export class Dep{
    constructor(){
        this.subs=[]
    }
    notify(){
        //浅拷贝
        //每次update都会增加新的依赖，然后依赖数组动态变长，就变成了死循环，所以先拷贝是为了只遍历修改前的那一部分依赖。
        const subs = [...this.subs]
        subs.forEach((e)=>e.update())
    }
    depend(){
        if(Dep.target){
            this.addDep(Dep.target);
        }
    }
    addDep(sub){
        this.subs.push(sub)
    }
}

Dep.target = null
const targetStack = []

export function pushTarget (_target) {
  if (Dep.target) targetStack.push(Dep.target)
  Dep.target = _target
}

export function popTarget () {
  Dep.target = targetStack.pop()
}